package service;


import com.alibaba.fastjson.JSON;
import common.pojo.Page;
import common.utils.JsonUtils;
import dao.ChatListDao;
import dao.ChatRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ChatRecord;
import pojo.MessageModel;
import pojo.form.ChatRecordQueryForm;

import java.util.Collections;

/**
 * Created by asus on 2018/11/22.
 */
@Service
public class ChatRecordService {
    @Autowired
    ChatRecordDao chatRecordDao;
    @Autowired
    ChatListDao chatListDao;

    public ChatRecord getChatRecord(Long customId, Long shopId) {
        return chatRecordDao.getChatRecord(customId, shopId);
    }

    public Page<ChatRecord> getChatRecord(ChatRecordQueryForm queryForm, int curPage, int pageSize) {
        Page<ChatRecord> page = chatRecordDao.getChatRecord(queryForm, curPage, pageSize);
        Collections.reverse(page.getResult());
        chatListDao.updateChatList(queryForm.getCustomId(), queryForm.getShopId());
        return page;
    }

    public Page<ChatRecord> getChatRecords(ChatRecordQueryForm queryForm, int curPage, int pageSize) {
        Page<ChatRecord> page = chatRecordDao.getChatRecord(queryForm, curPage, pageSize);
        Collections.reverse(page.getResult());
        chatListDao.updateChatList(queryForm.getCustomId(), queryForm.getShopId());
        MessageModel model=null;
        for (ChatRecord record:page.getResult()){
            model= JsonUtils.objectToPojo(JSON.parse(record.getMessage()),MessageModel.class);
            record.setMessage(model.getMessage());
        }
        return page;
    }

    public Long getUnRradMsg(Long shopId) {
        return chatListDao.getUnRradMsg(shopId);
    }
}
